// pages/taskDetail/taskDetail.js
import config from "@/config/config";
import getRole from "@/utils/getRoleType"

import {
  getPatrolTaskTemplate,
  getPatrolTaskPlanOne,
  putPatrolTaskPlan,
  createWorkOrderFXObjFromXF
} from "@/api/taskDetailApi";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: config.imgUrl,
    gridConfig: {
      column: 3,
      width: 190,
      height: 190,
    },

    // originFiles0: [],
    // originFiles1: [],
    // originFiles2: [],
    // originFiles: [],
    // content: "",

    form: [], // 任务详情表单
    reportForm: {
      describe: "",
      fileList: []
    },

    reportText: "",

    showConfirm: false,
    imageOnTop: false,
    showReport: false,

    reportFiles: [],

    taskDetailInfo: {},
    taskTemplate: [],
    roleType: ''
  },

  // 文本框输入
  onDetailChange(e) {
    let index = e.currentTarget.dataset.index;
    console.log(`${index}`);
    this.setData({
      [`form[${index}].content`]: e.detail.value,
      [`taskTemplate[${index}].content`]: e.detail.value,
    });
  },

  // 文件上传
  handleSuccess(e) {
    let that = this;
    console.log(e);
    const { index1 } = e.currentTarget.dataset;
    const { files } = e.detail;
    // console.log(files);
    let fileList = [];
    files.forEach((item) => {
      wx.uploadFile({
        url: "https://slimage.lmding.com/fireFighting/file/upFileMore",
        filePath: item.url,
        name: "file",
        formData: {
          user: "test",
        },
        success(res) {
          let data = JSON.parse(res.data);
          fileList.push(data.data.list[0]);
          console.log("fileList------", fileList);
          that.setData({
            [`taskTemplate[${index1}].fileList`]: fileList,
          });
        },
      });
    });

    this.setData({
      [`form[${index1}].files`]: files,
    });
  },

  // 文件移除
  handleRemove(e) {
    const { index1 } = e.currentTarget.dataset;
    const { index } = e.detail;
    console.log(e);

    this.data.form[index1].files.splice(index, 1);
    this.setData({
      form: this.data.form,
    });
  },

  // 文件上传预览
  handleClick(e) {
    console.log(e.detail.file);
  },

  // 案件上报 文件上传
  handleSuccessReport(e) {
    let that = this
    const { files } = e.detail;
    this.setData({
      ['reportForm.fileList']: files,
    });
    let fileList = [];
    files.forEach((item) => {
      wx.uploadFile({
        url: "https://slimage.lmding.com/fireFighting/file/upFileMore",
        filePath: item.url,
        name: "file",
        formData: {
          user: "test",
        },
        success(res) {
          let data = JSON.parse(res.data);
          fileList.push(data.data.list[0]);
          console.log("fileList------", fileList);
          that.setData({
            [`reportForm.fileList2`]: fileList,
          });
        },
      });
    });
  },

  handleRemoveReport(e) {
    const { index } = e.detail;
    const { reportForm } = this.data;
    reportForm.fileList.splice(index, 1);
    this.setData({
      ['reportForm.fileList']: reportForm.fileList
    });
  },

  onReportText(e) {
    this.setData({
      ['reportForm.describe']: e.detail.value,
    });
  },

  // 图片预览
  showImg(e) {
    // console.log(e.currentTarget.dataset);
    let { img, index } = e.currentTarget.dataset;
    console.log(img, index);
    wx.previewMedia({
      sources: img,
      current: index,
    });
  },

  submit(e) {
    // this.setData({ imageOnTop: true })
    // this.setData({ showReport: true })
    const { type } = e.currentTarget.dataset;
    const { form, taskDetailInfo } = this.data;
    // wx.showToast({
    //   icon: "none",
    //   title: "请完善表单",
    // });
    // console.log(form);
    if (type == 'JXZ' || type == 'YQKS') {
      let flag = false
      for (let i = 0; i < form.length; i++) {
        console.log(form[i]);
        if (form[i].content == "" || form[i].files.length == 0) {
          console.log('不通过-----------');
          flag = true
          break;
        }
      }

      if (flag) {
        wx.showToast({
          icon: "none",
          title: "请完善表单",
        });
      } else {
        this.setData({ showConfirm: true });

      }


      // this.setData({ showConfirm: true });
      //   console.log("fdsfdsfd");



    }

    if (type == 'YJS' && taskDetailInfo.insertWorkSystemFlage == 'N') { // 上报至工单 insertWorkSystemFlage == 'N'
      this.setData({ showReport: true })
      return
    }



    // putPatrolTaskPlan({ attachmentInfoList: this.data.taskTemplate }, `obj_id=${taskDetailInfo.patrolTaskPlanOid}`).then((res) => {
    //   console.log(res);
    // })
  },

  confirmDialog() {

    console.log("确定提交");
    this.setData({ showConfirm: false });
    putPatrolTaskPlan({ attachmentInfoList: this.data.taskTemplate }, `obj_id=${this.data.taskDetailInfo.patrolTaskPlanOid}`).then((res) => {
      console.log(res);
      if (res.status == 200) {
        wx.showToast({
          title: '提交成功',
          icon: "success",
          duration: 2000,
          success: () => {
            wx.navigateBack()
          }
        })
      }
    })
  },

  cancelDialog() {
    console.log("取消");
    this.setData({ showConfirm: false });
  },

  confirmDialog2() {
    wx.navigateBack()
    this.setData({ imageOnTop: false });
  },

  onReportPopChange(e) {
    this.setData({ showReport: e.detail.visible });
  },

  cancelReport() {
    this.setData({ showReport: false });
  },

  // 确定案件上报
  confirmReport() {
    const { taskDetailInfo, reportForm } = this.data
    createWorkOrderFXObjFromXF({ patrolTaskPlanOid: taskDetailInfo.patrolTaskPlanOid, describe: reportForm.describe, fileList: reportForm.fileList2 }).then((res) => {
      if (res.code == 20000) {
        this.setData({ imageOnTop: true })
        // wx.navigateBack()
      }
    })
    this.setData({ showReport: false });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let { obj_id } = options;
    console.log(obj_id);
    getPatrolTaskTemplate({ obj_id }).then((res) => {
      let templateList = res.data.attachmentInfoList;
      templateList.forEach((item) => {
        this.data.form.push({ content: "", files: [] });
      });
      this.setData({ taskTemplate: res.data.attachmentInfoList });
    });

    getPatrolTaskPlanOne({ obj_id }).then((res) => {
      this.setData({ taskDetailInfo: res.data });
    });
    //
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let roleType = getRole()
    console.log(roleType);
    this.setData({ roleType })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
